﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DrWileyO_REDACTED_Adventure
{
    public class AnimatedSprite
    {
        public Vector2 position = Vector2.Zero;
        public Texture2D Texture { get; set; }
        public int Rows { get; set; }
        public int Columns { get; set; }
        public int currentFrame { get; set; }
        public int Height;
        public int Width;
        public Rectangle size;
        public int MaxX, MaxY, MinX, MinY;
        public Rectangle srcRect;
        public Rectangle destRect;
        private float scale = 1.0f;

        public float Scale
        {
            get { return scale; }
            set
            {
                scale = value;
                setDestRect();
            }
        }

        public AnimatedSprite(Texture2D texture, Vector2 pos, int rows, int columns)
        {
            position = pos;
            setTexture(texture, rows, columns);
        }

        public void setTexture(Texture2D texture, int rows, int columns)
        {
            this.Texture = texture;
            this.Rows = rows;
            this.Columns = columns;

            Height = texture.Height / rows;
            Width = texture.Width / columns;
        }

        public virtual void LoadContent(ContentManager content, string name, int Rows, int Columns)
        {
            Texture = content.Load<Texture2D>(name);

            this.Rows = Rows;
            this.Columns = Columns;

            Height = Texture.Height / Rows;
            Width = Texture.Width / Columns;
        }

        public void SetScreenBounds(int scrnWidth, int scrnHeight)
        {
            MinX = MinY = 0;

            MaxX = scrnWidth - Width;
            MaxY = scrnHeight - Height;
        }

        public void Update(GameTime time, Vector2 newPos)
        {
            position = newPos;
        }

        public virtual void Draw(SpriteBatch spriteBatch, bool flip)
        {
            int rowTexture = (int)((float)currentFrame / (float)Columns);
            int columnTexture = currentFrame % Columns;

            srcRect = new Rectangle(Width * columnTexture, Height * rowTexture, Width, Height);
            setDestRect();

            if(flip)
                spriteBatch.Draw(Texture, position, srcRect, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.FlipHorizontally, .7f);
            else
                spriteBatch.Draw(Texture, position, srcRect, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, .7f);
        }

        public virtual void Draw(SpriteBatch spriteBatch, Color color, bool flip)
        {
            int rowTexture = (int)((float)currentFrame / (float)Columns);
            int columnTexture = currentFrame % Columns;

            srcRect = new Rectangle(Width * columnTexture, Height * rowTexture, Width, Height);
            setDestRect();

            if(flip)
                spriteBatch.Draw(Texture, position, srcRect, color, 0f, Vector2.Zero, scale, SpriteEffects.FlipHorizontally, .7f);
            else
                spriteBatch.Draw(Texture, position, srcRect, color, 0f, Vector2.Zero, scale, SpriteEffects.None, .7f);
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            int rowTexture = (int)((float)currentFrame / (float)Columns);
            int columnTexture = currentFrame % Columns;

            srcRect = new Rectangle(Width * columnTexture, Height * rowTexture, Width, Height);
            setDestRect();

            spriteBatch.Draw(Texture, position, srcRect, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, .7f);
        }

        public virtual void Draw(SpriteBatch spriteBatch, Color color)
        {
            int rowTexture = (int)((float)currentFrame / (float)Columns);
            int columnTexture = currentFrame % Columns;

            srcRect = new Rectangle(Width * columnTexture, Height * rowTexture, Width, Height);
            setDestRect();

            spriteBatch.Draw(Texture, position, srcRect, color, 0f, Vector2.Zero, scale, SpriteEffects.None, .7f);
        }

        public void setDestRect()
        {
            destRect = new Rectangle((int)(position.X - (Scale * Width * 0.5f)), (int)(position.Y - (Scale * Height * 0.5f)), (int)(Width * Scale), (int)(Height * Scale));
        }
    }
}
